﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace server.app.table
{
    public class Faction : Table<comm.objst.TableFaction>
    {
        public const string TABLE_LORD_VOTE = Conf.TABLE_NAME_PREFIX + "LordVote";
        public const string TABLE_COMMANDER = Conf.TABLE_NAME_PREFIX + "Commander";
        public const string TABLE_LORD_HISTORY = Conf.TABLE_NAME_PREFIX + "LordHistory";
        public const string TABLE_COMMANDER_HISTORY = Conf.TABLE_NAME_PREFIX + "CommanderHistory";

        Table<comm.objst.TableLordVote> _tableLordVote = null;
        Table<comm.objst.TableCommander> _tableCommander = null;
        Table<comm.objst.TableLordHistory> _tableLordHsy = null;
        Table<comm.objst.TableCommanderHistory> _tableCommanderHsy = null;

        public Faction(World world)
            : base(Conf.TABLE_NAME_PREFIX + typeof(Faction).Name, world)
        {
            _tableLordVote = new Table<comm.objst.TableLordVote>(TABLE_LORD_VOTE, world);
            _tableCommander = new Table<comm.objst.TableCommander>(TABLE_COMMANDER, world);
            _tableLordHsy = new Table<comm.objst.TableLordHistory>(TABLE_LORD_HISTORY, world);
            _tableCommanderHsy = new Table<comm.objst.TableCommanderHistory>(TABLE_COMMANDER_HISTORY, world);
        }

        public void UpdateStat(string facId, uint heroCount = 0)
        {
            _world.DataManager.Call(sqlx =>
            {
                var fac = Orm.GetObjectByKey(facId);
                if (fac != null)
                {
                    fac.HeroCount += (int)heroCount;
                    Orm.Update(fac);
                }
            });
        }
        public bool CreateFaction(SQLx sqlx, comm.objst.TableFaction fac)
        {
            return Orm.Insert(sqlx, fac);
        }

        public comm.objst.TableFaction GetFaction(string factionId)
        {
            return Orm.GetObjectByKey(factionId);
        }
        public bool UpdateFaction(comm.objst.TableFaction tableFaction)
        {
            return Orm.Update(tableFaction);
        }

        public override string CreateTableSql()
        {
            return base.CreateTableSql() +
                _tableLordVote.Orm.CreateTableSql() +
                _tableCommander.Orm.CreateTableSql() +
                _tableLordHsy.Orm.CreateTableSql() +
                _tableCommanderHsy.Orm.CreateTableSql();
        }
    }
}
